/*
 * @Description: 多边形（三维数组）
 * @Date: 2022-03-22 12:07:43
 * @Author: yeming
 */
import OlFeature from 'ol/Feature'
import OlGeomPolygon from 'ol/geom/Polygon';
import { fromLonLat } from 'ol/proj';
import { setOlStylePolygon } from '../../common/olStyle.js'

// 默认蓝色区域
const DEFAULT_POLYGON_STYLE = { 'fillColor': 'rgba(0, 153, 255, 0.1)', 'strokeColor': 'rgba(0, 153, 255, 1)', 'strokeWidth': 1 }
// 禁飞区红色区域
const FORBID_POLYGON_STYLE = { 'fillColor': 'rgba(255, 102, 102, 0.2)', 'strokeColor': 'rgba(255, 102, 102, 1)', 'strokeWidth': 1 }
// 电子围栏橙色区域
const FENCE_POLYGON_STYLE = { 'fillColor': 'rgba(255,165,0,0.2)', 'strokeColor': 'rgba(255,165,0,1)', 'strokeWidth': 1 }

class Polygon3d {
  constructor(entities, id, type, latlng, style, clampToGround = false) {
    let customStyle = {}
    if (!style || JSON.stringify(style) == "{}") {
      if (type == 5 || type == 6) customStyle = DEFAULT_POLYGON_STYLE
      else customStyle = DEFAULT_POLYGON_STYLE
    } else customStyle = style

    let that = this
    this.entities = entities
    this.positions = []

    this.options = {
      id: id, // 创建和2d线相同sessionId的entity,
      name: 'Polygon3d_' + id,
      show: true,
      polygon: {
        material: Cesium.Color.fromCssColorString(customStyle.fillColor),
        clampToGround: clampToGround,
        height: 0,
        outlineColor: Cesium.Color.fromCssColorString(customStyle.strokeColor),
        outlineWidth: customStyle.strokeWidth,
        outline: true,
        hierarchy: this.positions
      }
    }

    this.options.polygon.hierarchy = new Cesium.CallbackProperty(function () {
      if (that.positions.length > 2) return new Cesium.PolygonHierarchy(that.positions);
    }, false)

    this.entity = this.entities.add(this.options)
  }

}

class Polygon {
  constructor(ol2d, vectorSource) {
    this.ol2d = ol2d
    this.vectorSource = vectorSource // 矢量标注的数据源 addFeature(), getFeatures(), removeFeature()
    this.feature = null
    this.forbidPolygon = null
    this.electricFencePolygon = null
    this.polygonArea = null
  }

  /**
   * @desc: 渲染禁飞区
   * @param {Number} id
   * @param {Array} coordinates [[[x, y], [x, y], ......, [x, y]], [[x, y], [x, y], ......, [lon, y]]]
   */
  renderPolygonForbid (id, coordinates) {
    this.forbidPolygon = new OlGeomPolygon(coordinates)
    const feature = new OlFeature({
      id: id,
      type: 'forbidPolygon',
      rend3d: true,
      geometry: this.forbidPolygon
    })
    this.feature = feature
    feature.setStyle(setOlStylePolygon(FORBID_POLYGON_STYLE))
    this.vectorSource.addFeature(feature)
  }

  drawPolygonElectricFence (id, latlng) {
    this.electricFencePolygon = new OlGeomPolygon([latlng]).transform("EPSG:4326", "EPSG:3857")
    const feature = new OlFeature({
      id: id,
      type: 'electricFencePolygon',
      rend3d: true,
      geometry: this.electricFencePolygon
    })
    this.feature = feature
    feature.setStyle(setOlStylePolygon(FENCE_POLYGON_STYLE))
    this.vectorSource.addFeature(feature)
  }

  renderSimulationPolygon (id, latlng, width, height) {
    this.polygonArea = new OlGeomPolygon(latlng).transform("EPSG:4326", "EPSG:3857")
    const feature = new OlFeature({
      id: id,
      width: width,
      height: height,
      rend3d: true,
      geometry: this.polygonArea
    })
    this.feature = feature
    feature.setStyle(setOlStylePolygon(DEFAULT_POLYGON_STYLE))
    this.vectorSource.addFeature(feature)
  }
}

export {
  Polygon,
  Polygon3d
}